package com.learn.demo.interceptor;

import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author banjiawei
 * @date 2020/08/24
 * forest拦截器
 */
@Slf4j
public class DemoInterceptor implements Interceptor {
    @Override
    public boolean beforeExecute(ForestRequest forestRequest) {
        log.info("invoke Simple beforeExecute");
        return true;
    }

    @Override
    public void onSuccess(Object o, ForestRequest forestRequest, ForestResponse forestResponse) {
        log.info("invoke Simple onSuccess");
    }

    @Override
    public void onError(ForestRuntimeException e, ForestRequest forestRequest, ForestResponse forestResponse) {
        log.info("invoke Simple onError");
        // 获取请求响应状态码
        int status = forestResponse.getStatusCode();
        // 获取请求的响应内容
        String content = forestResponse.getContent();
        // 获取方法返回类型对应的返回数据结果
        Object result = forestResponse.getResult();
        log.info("{},{},{}",status, content, result);
    }

    @Override
    public void afterExecute(ForestRequest forestRequest, ForestResponse forestResponse) {
        log.info("invoke Simple afterExecute");
    }
}
